package com.dashboard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.security.userdetails.jdbc.JdbcDaoImpl;

import com.dashboard.dao.UsuarioDao;

public class DashboardUserDetailsService extends JdbcDaoImpl 
{
	
	UsuarioDao userDao;
	
	/**
	 * Construtor com os servi&ccedil;os necess&aacute;rios para inje&ccedil;&atilde;o.
	 * 
	 * @param userDao dao de usu&aacute;rios
	 */
	@Autowired
	public DashboardUserDetailsService(UsuarioDao userDao)
	{
		this.userDao = userDao;
	}
	
	/**
	 * bla.
	 * @param username nome do usuario.
	 * 
	 * @return o usuario.
	 * @throws UsernameNotFoundException caso usuario nao seja encontrado
	 * @throws DataAccessException problemas no acesso ao banco
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
		throws UsernameNotFoundException, DataAccessException 
	{
		try 
		{
			return userDao.recuperarPorNomeUsuario(username);
		}
		catch (UsernameNotFoundException ex1) 
		{
			ex1.printStackTrace();
			throw ex1;
		} 
		catch (DataAccessException ex2) 
		{
			ex2.printStackTrace();
			throw ex2;
		}
	}
}
